package com.xuecheng.content.controller;

import com.xuecheng.api.content.CourseBaseApi;
import com.xuecheng.api.content.model.dto.CourseBaseDTO;
import com.xuecheng.api.content.model.qo.QueryCourseModel;
import com.xuecheng.api.content.model.vo.CourseBaseVO;
import com.xuecheng.common.domain.page.PageRequestParams;
import com.xuecheng.common.domain.page.PageVO;
import com.xuecheng.content.common.util.UAASecurityUtil;
import com.xuecheng.content.convert.CourseBaseConvert;
import com.xuecheng.content.service.CoursePubService;
import org.springframework.web.bind.annotation.*;
import com.xuecheng.content.service.CourseBaseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.ModelAndView;

import java.util.Map;

/**
 * 课程基本信息 前端控制器
 *
 * @author dongliang
 */
@Slf4j
@RestController
public class CourseBaseController implements CourseBaseApi {

    @Autowired
    private CourseBaseService courseBaseService;
    @Autowired
    private CoursePubService coursePubService;

    /**
     * 带条件分页查询
     *
     * @param params 封装分页数据
     * @param model  前端传入课程基础信息 Qo
     * @return PageVO 分页结果封装
     */
    @PostMapping("course/list")
    public PageVO queryCourseList(PageRequestParams params,
                                  @RequestBody QueryCourseModel model) {
        //1.获取令牌id
        Long companyId = UAASecurityUtil.getCompanyId();
        //调用service分页查询,PageVO接收
        PageVO<CourseBaseDTO> pageVO = courseBaseService.queryCourseList(params, model, companyId);

        return pageVO;
    }

    /**
     * 新增课程基本信息
     *
     * @param vo 添加封装的数据接收前端数据
     * @return courseBaseDTO 新增课程基本信息结果
     */
    @PostMapping("course")
    public CourseBaseDTO createCourseBase(@RequestBody CourseBaseVO vo) {
        //1获取令牌id
        Long companyId = UAASecurityUtil.getCompanyId();
        //2.vo2dto
        CourseBaseDTO dto = CourseBaseConvert.INSTANCE.vo2dto(vo);
        dto.setCompanyId(companyId);
        //3.调用service添加课程基本信息
        CourseBaseDTO courseBaseDTO = courseBaseService.createCourseBase(dto);

        return courseBaseDTO;
    }

    /**
     * 根据课程Id查询此需更新课程信息
     *
     * @param courseBaseId 课程ID
     * @return courseBaseDTO  根据Id查询此课程基本信息结果
     */
    @GetMapping("course/{courseBaseId}")
    public CourseBaseDTO getCourseBase(@PathVariable Long courseBaseId) {
        //1获取令牌id
        Long companyId = UAASecurityUtil.getCompanyId();
        //2.调用service根据Id查询此课程
        CourseBaseDTO courseBaseDTO = courseBaseService.getCourseBase(courseBaseId, companyId);

        return courseBaseDTO;
    }

    /**
     * 更新录播课程基本信息
     *
     * @param courseBaseVO 更新的封装的数据接收前端数据
     * @return 更新后课程基本信息结果
     */
    @PutMapping("course")
    public CourseBaseDTO modifyCourseBase(@RequestBody CourseBaseVO courseBaseVO) {
        //1获得公司id
        Long companyId = UAASecurityUtil.getCompanyId();
        //2.vo2dto
        CourseBaseDTO courseBaseDTO = CourseBaseConvert.INSTANCE.vo2dto(courseBaseVO);
        courseBaseDTO.setCompanyId(companyId);
        //3.调用service更新课程基本信息
        CourseBaseDTO resultCourse = courseBaseService.modifyCourseBase(courseBaseDTO);
        return resultCourse;
    }

    /**
     * 删除录播课程基本信息
     *
     * @param courseBaseId 课程id
     * @return 删除结果true false
     */
    @DeleteMapping("course/{courseBaseId}")
    public boolean removeCourseBase(@PathVariable Long courseBaseId) {
        //1获得公司id
        Long companyId = UAASecurityUtil.getCompanyId();
        //2.调用service删除
        boolean flag = courseBaseService.removeCourseBaseById(courseBaseId, companyId);
        return flag;
    }


    /**
     * 根据课程Id提交课程信息
     *
     * @param courseBaseId 课程id
     */
    @GetMapping("course/commit/{courseBaseId}")
    public void commitCourseBase(@PathVariable Long courseBaseId) {
        //1获得公司id
        Long companyId = UAASecurityUtil.getCompanyId();
        //2.调用service删除
        courseBaseService.commitCourseBase(courseBaseId, companyId);
    }

    /**
     * 课程预览
     *
     * @param courseId  课程Id
     * @param companyId 公司Id
     * @return Map 数据模型 Map
     */
    /*
     * SpringMVC 中controller 多结果视图器并存
     *      CourseBaseController ：
     *           JackSonResolver （默认）--controller使用的是RestController
     *           FreemarkerViewResolver
     *               1.添加依赖：spring-boot-starter-Freemarker
     *               2.添加配置：spring.freemarker.enabled = true
     *               3.方法的返回：controller的方法返回为ModelAndView对象
     * */
    @GetMapping("course/preview/{courseId}/{companyId}")
    public Object previewCourse(@PathVariable Long courseId, @PathVariable Long companyId) {

        Map<String, Object> dataMap = coursePubService.previewCourse(courseId, companyId);

        // 1.使用视图和数据模型对象
        ModelAndView modelAndView = new ModelAndView("learing_article", dataMap);

        return modelAndView;
    }

    /**
     * 课程发布
     * @param courseId 课程id
     */
    @PostMapping("course_pub/publish/{courseId}")
    public void publish(@PathVariable Long courseId) {

        Long companyId = UAASecurityUtil.getCompanyId();
        coursePubService.publish(courseId, companyId, false);

    }
}